package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.dto.Simple;
import com.itheima.dto.SubjectsDto;
import com.itheima.pojo.Subjects;
import com.itheima.pojo.User;
import com.itheima.service.SubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/subjects")
public class SubjectsController {


    @Autowired
    private SubjectsService subjectsService;

    @Autowired
    private HttpServletRequest req;

    /**
     *
     * Description:
     *学科列表
     * @param:  * @param subjectName
     * @param page
     * @param pagesize
     * @return: {@link PageResult}
     */
    @GetMapping
    public PageResult subjectsPage(String subjectName, Integer page, Integer pagesize){
            return subjectsService.subjectsPage(subjectName,page,pagesize);
    }

    /**
     *
     * Description:
     *学科修改
     * @param:  * @param id
     * @param subjects
     * @return: {@link String}
     */
    @PutMapping("/{id}")
    public String modifySubjectsByid(@PathVariable Integer id, @RequestBody Subjects subjects){

        subjectsService.modifySubjectsByid(subjects);

        return "{'success':true}";
    }


    /**
     *
     * Description:
     *学科删除
     * @param:  * @param id
     * @param subjects
     * @return: {@link String}
     */
    @DeleteMapping("/{id}")
    public String removeSubjectsByid(@PathVariable Integer id, @RequestBody Subjects subjects){
        subjectsService.removeSubjectsByid(id);

        return "{'success':false}";
    }

    /**
     *
     * Description:
     *学科添加
     * @param:  * @param subjects
     * @return: {@link String}
     */
    @PostMapping
    public String addSubjects(@RequestBody Subjects subjects){

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        subjects.setAddDate(LocalDateTime.now());
        subjects.setCreatorID(user.getId());
        subjects.setTags(0);
        subjects.setTotals(0);
        subjects.setTwoLevelDirectory(0);
        subjects.setIsFrontDisplay(1);
        Integer id = subjectsService.addSubjects(subjects);

        return "{'id':"+id+"}";
    }

    /**
     *
     * Description:
     *学科简单列表
     * @param:  * @param
     * @return: {@link List<   Simple  >}
     */
    @GetMapping("/simple")
    public List<Simple> simple(){
        List<Simple> list =  subjectsService.simple();

       return list;
    }

    /**
     *
     * Description:
     *学科详情
     * @param:  * @param id
     * @return: {@link Subjects}
     */
    @GetMapping("/{id}")
    public SubjectsDto findSubjectsById(@PathVariable Integer id){
        SubjectsDto subjectsDto = subjectsService.findSubjectsById(id);
        return subjectsDto;
    }

}
